package cn.edu.cug.cs.gtl.docsrv.demo.controller;

import cn.edu.cug.cs.gtl.docsrv.Managers;
import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.io.File;
import cn.edu.cug.cs.gtl.mybatis.mapper.doc.DocAll;
import cn.edu.cug.cs.gtl.protos.BoundingBox;
import cn.edu.cug.cs.gtl.protos.Document;
import cn.edu.cug.cs.gtl.protos.Value;
import cn.edu.cug.cs.gtl.protoswrapper.BoundingBoxWrapper;
import cn.edu.cug.cs.gtl.protoswrapper.DocumentWrapper;
import cn.edu.cug.cs.gtl.protoswrapper.ValueWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.protobuf.ByteString;
import org.apache.catalina.connector.RequestFacade;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.print.Doc;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.util.Base64;

@CrossOrigin
@RestController
public class HelloController {

    @GetMapping(value = "/api/demo/hello")
    public String hello(
            @RequestParam(value = "name", defaultValue = "World") String name){
        return "Hello "+name;
    }

    @GetMapping(value = "/api/demo/getString")
    public String getString(
            @RequestParam(value = "name", defaultValue = "World") String name){
        return "Hello "+name;
    }

    @GetMapping(value = "/api/demo/getLocalDataRoot")
    public String getLocalDataRoot(){
        return Configuration.getManagerConfig().getLocalDataRoot();
    }

    @GetMapping(value = "/api/demo/getDocAll")
    public DocAll getDocAll(
            @RequestParam(value = "name", defaultValue = "World") String name){
        DocAll docAll = new DocAll();
        docAll.setDocEnTitle("hello");
        docAll.setDocUrl("http://localhost");
        return docAll;
    }



    @GetMapping(value = "/api/demo/getValue")
    public byte[] getValue(){
        return ValueWrapper.of("This is 测试值！").toByteString().toByteArray();
    }

    @PostMapping(value = "/api/demo/addDocument")
    public byte[] addDocument( @RequestBody byte[] data){
        byte[] res=new byte[1];
        try {
            System.out.println(data.length);
            Document document = Document.parseFrom(data);
            //TODO ......


            res[0]=1;//1-成功，0-失败
            return res;
        }
        catch (Exception e){
            e.printStackTrace();
            res[0]=0;//1-成功，0-失败
            return res;
        }
    }

    @RequestMapping(value = "/api/demo/uploadFile", method = RequestMethod.POST)
    public String uploadFile(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        if(request instanceof MultipartHttpServletRequest){
            MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartRequest.getFile("file1");//file是form-data中二进制字段对应的name
            System.out.println(multipartFile.getSize());
        }
        else if(request instanceof RequestFacade){
            StringBuffer jb = new StringBuffer();
            String line = null;
            try {
                BufferedReader reader = request.getReader();
                while ((line = reader.readLine()) != null)
                    jb.append(line);

                System.out.println(jb.toString());


            } catch (Exception e) { /*report an error*/ }
        }

        return "ok";
    }



    @GetMapping(value = "/api/demo/getDocument")
    public byte[] getDocument(){
        String url = "/Users/zhenwenhe/git/data/docsrv_data/zip/01_平面图_dwg_Polyline.zip";
        try {
            ByteString bs = ByteString.copyFrom(File.readRawFile(url).array());
            Document document = Document.newBuilder()
                    .setInternalId(1L)
                    .setTitle("test")
                    .setComment("测试")
                    .setAbstract("test abstract")
                    .addAffiliation("CUG")
                    .addAffiliation("CUG")
                    .addAffiliation("WHU")
                    .addKeyword("test")
                    .addKeyword("key")
                    .addKeyword("word")
                    .addAuthor("HZW1")
                    .addAuthor("HZW2")
                    .addAuthor("HZW3")
                    .setUrl("file:"+url)
                    .addBounding(BoundingBoxWrapper.of(1.0,1.0,100.0,100.0))
                    .setRawData(bs)
                    .addContent("content 1")
                    .addContent("content 2")
                    .setMemo("memo")
                    .build();
            byte[] ba = document.toByteArray();
            return ba;
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


}
